package com.qzsoft.zhjd.machine.zhgcloud;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;

import cn.hutool.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName ZhgHandler
 * @Description 机械指挥官处理
 * @author linhuawei
 * @since 2021-11-04
 */
@Slf4j
@Component
public class ZhgHandler {

    @Value("${zhgcloud.appKey}")
    private String appKey;

    @Value("${zhgcloud.appSecret}")
    private String appSecret;

    @Value("${zhgcloud.tenantKey}")
    private String tenantKey;

    /**
     * @Title httpGetObject
     * @Description 通过get请求调用机械指挥官API接口，获取数据对象
     * @param url      API地址
     * @param params   请求参数
     * @param clasz    结果对象
     * @param callback 数据处理回调
     * @return API返回数据
     */
    public <T> T httpGetObject(String url, Map<String, Object> params, Class<T> clazz, ZhgResultDataCallback callback) {
        // 追加参数
        params.put("app_key", appKey);
        params.put("app_secret", appSecret);
        params.put("tenant_key", tenantKey);

        // 执行http请求
        try {
            String result = HttpUtil.get(url, params);
            JSONObject resultJson = JSONObject.parseObject(result);
            Integer status = resultJson.getInteger("status");
            if (null != status && 0 == status.intValue()) {
                log.info("zhg-接口调用成功:url = {}, params = {}", url, params);
                String data = callback.callback(resultJson);
                if (ObjectUtils.isNotEmpty(data)) {
                    return JSONObject.parseObject(data, clazz);
                }
            } else {
                log.error("zhg-接口调用失败:url = {}, params = {}, result = {}", url, params, result);
            }
        } catch (Exception e) {
            log.error("zhg-接口调用异常:url = {}, params = {}, ex = {}", url, params, e.getMessage());
        }
        return null;
    }

    /**
     * @Title httpGetList
     * @Description 通过get请求调用机械指挥官API接口，获取数据列表
     * @param url      API地址
     * @param params   请求参数
     * @param clazz    结果对象
     * @param callback 数据处理回调
     * @return API返回数据
     */
    public <T> List<T> httpGetList(String url, Map<String, Object> params, Class<T> clazz,
            ZhgResultDataCallback callback) {
        // 追加参数
        params.put("app_key", appKey);
        params.put("app_secret", appSecret);
        params.put("tenant_key", tenantKey);

        // 执行http请求
        try {
            String result = HttpUtil.get(url, params);
            JSONObject resultJson = JSONObject.parseObject(result);
            Integer status = resultJson.getInteger("status");
            if (null != status && 0 == status.intValue()) {
                String data = callback.callback(resultJson);
                log.info("zhg-接口调用成功:url = {}, params = {}, result = {}", url, params, result);
                if (ObjectUtils.isNotEmpty(data)) {
                    return JSONArray.parseArray(data, clazz);
                }
            } else {
                log.error("zhg-接口调用失败:url = {}, params = {}, result = {}", url, params, result);
            }
        } catch (Exception e) {
            log.error("zhg-接口调用异常:url = {}, params = {}, ex = {}", url, params, e.getMessage());
        }
        return null;
    }

}
